package com.lyf.shop.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.lyf.common.core.utils.DateUtils;
import com.lyf.common.core.utils.StringUtils;
import com.lyf.shop.domain.Commodity;
import com.lyf.shop.domain.OrderRecord;
import com.lyf.shop.mapper.OrderRecordMapper;
import com.lyf.shop.util.AliPayStatusEnum;
import com.lyf.shop.vo.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lyf.shop.mapper.OrderMapper;
import com.lyf.shop.domain.Order;
import com.lyf.shop.service.IOrderService;

/**
 * 订单Service业务层处理
 * 
 * @author lyf
 * @date 2023-03-22
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderRecordMapper recordMapper;
    /**
     * 查询订单
     * 
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public Order selectOrderByOrderId(Long orderId)
    {
        return orderMapper.selectOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表
     * 
     * @param order 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    @Override
    public int insertOrderBatch(TradeVo trade) {
        Order order = new Order();
        order.setSupermarketId(trade.getSupermarketId());
        order.setOutTradeNo(trade.getOutTradeNo());
        order.setTradeNo(trade.getTradeNo());
        order.setNumberId(trade.getNumber());
        order.setIntegralStatus(trade.getStatusIntegral());
        order.setStatus(AliPayStatusEnum.BUYER_PAY.getValue());
        order.setOrderNum(trade.getOrderNum());
        order.setAmount(trade.getTotalAmount());
        //保存订单信息
        int row = insertOrder(order);
        //保存订单记录表中的订单信息
        Order order1 = selectOrderByOutTradeNo(order.getOutTradeNo());
        trade.setOrderId(order1.getOrderId());
        // 保存订单下的商品信息
        insertOrderRecordBatch(trade);
        return row;

    }


    /**
     * 保存订单中的商品信息
     * @param trade
     */
    public void insertOrderRecordBatch(TradeVo trade){

        List<Commodity> commodityList = trade.getCommodityList();
        if (StringUtils.isNotEmpty(commodityList))
        {
            // 新增用户与超市管理
            List<OrderRecord> list = new ArrayList<OrderRecord>();
            for (Commodity commodity : commodityList)
            {
                OrderRecord or = new OrderRecord();
                or.setOrderId(trade.getOrderId());
                or.setCommodityId(commodity.getId());
                or.setName(commodity.getName());
                or.setPicture(commodity.getPicture());
                or.setPrice(commodity.getPrice());
                or.setNum(commodity.getCommodityNum());
                list.add(or);
            }
            recordMapper.batchUserOrderRecord(list);
        }
    }
    /**
     * 新增订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }


    /**
     * 根据生成的id查询订单
     * @param outTradeNo
     * @return
     */
    public Order selectOrderByOutTradeNo(String outTradeNo){

      return   orderMapper.selectOrderByOutTradeNo(outTradeNo);
    }

    /**
     * 修改订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        order.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单
     * 
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderIds(Long[] orderIds)
    {
        return orderMapper.deleteOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     * 
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderId(Long orderId)
    {
        return orderMapper.deleteOrderByOrderId(orderId);
    }
}
